{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Gmail Toolkit\n",
    "\n",
    "**The Gmail Toolkit** allows you to create drafts, send email, and search for messages and threads using natural language.\n",
    "\n",
    "As a prerequisite, you will need to register with Google and generate a `credentials.json` file in the directory where you run this loader. See [here](https://developers.google.com/workspace/guides/create-credentials) for instructions.\n",
    "\n",
    "This example goes over how to use the Gmail Toolkit:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.llms import OpenAI\n",
    "from langchain.agents.agent_toolkits.gmail.base import create_gmail_agent\n",
    "import json\n",
    "\n",
    "llm = OpenAI(verbose=True)\n",
    "gmail_agent = create_gmail_agent(llm=llm, sender_name=\"Alice\", verbose=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "command = \"search for all messages during november 2022\"\n",
    "output = gmail_agent.run(command)\n",
    "\n",
    "messages = json.loads(output)\n",
    "\n",
    "print(\"Messages:\")\n",
    "for message in messages:\n",
    "  print(f\"{message['id']}: {message['snippet']}\")\n",
    "\n",
    "id = messages[0][\"id\"]\n",
    "\n",
    "command = f\"get the body for message id {id}\"\n",
    "\n",
    "output = gmail_agent.run(command)\n",
    "\n",
    "print(f\"Message body: {output}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "command = \"create a draft email to bob@example.com explaining why I can't make the meeting next week.\"\n",
    "output = gmail_agent.run(command)\n",
    "\n",
    "print(output)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "agent-ui",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.8"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
